package com.huawei.account.controller;

import com.huawei.account.entity.request.LoginReq;
import com.huawei.account.entity.request.ExplainTokenReq;
import com.huawei.account.entity.response.*;
import com.huawei.account.service.HuaweiLoginService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
@Slf4j
public class LoginController {
    private final HuaweiLoginService huaweiLoginService;

    /**
     * 华为登录接口
     */
    @PostMapping("/login")
    public ApiResponse<HuaweiTokenResp> login(@Valid @RequestBody LoginReq loginReq) {
        try {
            log.info("华为登录请求: authorizationCode={}", loginReq.getAuthorizationCode());
            //获取应用级accessToken凭证
            huaweiLoginService.getAPPAccessToken();

            //获取用户级accessToken凭证
            HuaweiTokenResp Resp = huaweiLoginService.loginWithAuthorizationCode(loginReq.getAuthorizationCode());
            HuaweiTokenResp RespToken = new HuaweiTokenResp();
            RespToken.setAccess_token(Resp.getAccess_token());
            RespToken.setRefresh_token(Resp.getRefresh_token());
            RespToken.setScope(Resp.getScope());

            return ApiResponse.success("authorizationCode校验成功", RespToken);

        } catch (Exception e) {
            log.error("登录失败", e);
            return ApiResponse.error("登录失败: " + e.getMessage());
        }
    }

    /**
     * 华为登录接口
     */
    @PostMapping("/check-token")
    public ApiResponse<String> checkToken(@Valid @RequestBody ExplainTokenReq refreshTokenReq) {
        try {
            //解析用户级accessToken凭证
            HuaweiExplainAccessResp ExplainAccessToken = huaweiLoginService.ExplainAccessToken(refreshTokenReq.getAccess_token());
            if(ExplainAccessToken.getClient_id() != null) {
            return ApiResponse.success("校验成功");
            }else {
                return ApiResponse.error("校验失败: ");
            }
        } catch (Exception e) {
            log.error("校验失败", e);
            return ApiResponse.error("登录失败: " + e.getMessage());
        }
    }

    /**
     * 刷新令牌接口
     */
    @PostMapping("/refresh-token")
    public ApiResponse<HuaweiRefreshTokenResp> refreshToken(@Valid @RequestBody HuaweiTokenResp refreshTokenReq) {
        try {
            log.info("刷新令牌请求");
            HuaweiRefreshTokenResp result = huaweiLoginService.refreshAccessToken(refreshTokenReq);
            return ApiResponse.success("令牌刷新成功", result);
        } catch (Exception e) {
            log.error("令牌刷新失败", e);
            return ApiResponse.error("令牌刷新失败: " + e.getMessage());
        }
    }


    /**
     * 获取用户信息（token解析成功才调用）
     */
    @PostMapping("/get-userInfo")
    public ApiResponse<HuaweiGetUserInfoResp> getUserInfo(@Valid @RequestBody ExplainTokenReq accessTokenRes) {
        try {
            HuaweiGetUserInfoResp userInfo = huaweiLoginService.GetUserInfo(accessTokenRes.getAccess_token());
            return ApiResponse.success("获取用户信息成功", userInfo);
        } catch (Exception e) {
            return ApiResponse.error("获取用户信息失败: " + e.getMessage());
        }
    }


    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public ApiResponse<String> logout(@Valid @RequestBody ExplainTokenReq token) {
        try {
            log.info("登出请求");
            //使令牌失效
            huaweiLoginService.LoginOut(token.getAccess_token());
            return ApiResponse.success("登出成功");
        } catch (Exception e) {
            log.error("登出失败", e);
            return ApiResponse.error("登出失败: " + e.getMessage());
        }
    }
}
